<template>
  <!--核销案件明细-->
  <section class="page five-level-return-rate-report">
    <data-form :submitLoading="submitLoading" :model="searchModel" @onSearch="refreshData" :page="pageService">
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="searchModel.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="时间" prop="caseFollowInTime">
          <el-date-picker type="daterange" :editable="false" v-model="searchModel.caseFollowInTime" placeholder="选择日期时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="分类" prop="caseSort">
          <el-select placeholder="请选择" v-model="searchModel.caseSort" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('CaseSort')" :key="index" :label="name" :value="code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="系统来源" prop="sysCode">
          <el-select placeholder="请选择" v-model="searchModel.sysCode" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('SysCode')" :key="index" :label="name" :value="code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="放款渠道" prop="loanChannel">
          <el-select placeholder="请选择" v-model="searchModel.loanChannel" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('LoanChannel')" :key="index" :label="name" :value="code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="批次号" prop="verifyBatchNumber">
         <el-input v-model="searchModel.verifyBatchNumber"></el-input>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="dataSet" :selectionList.sync="selectionList" @onPageChange="refreshData" :page="pageService">
      <template slot="buttons">
        <el-button @click="downloadVerifiedCase" v-auth = "1904">案件下载</el-button>
      </template>
      <template slot="columns">
        <!--数据列区域-->
        <el-table-column prop="verifyBatchNumber" label="批次号" :min-width="$helper.getColumnWidth(6)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="applyDate" label="申请时间" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{scope.row.applyDate | dateTimeFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="caseNumber" label="借据号" :min-width="$helper.getColumnWidth(6)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="personalName" label="客户名称" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="mobileNo" label="联系方式" :formatter="(row)=>$filter.encryptPhone(row.mobileNo)" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="idCard" label="证件号码" :formatter="(row)=>$filter.encryptIDCardFour(row.idCard)" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="area" label="地区" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $city.getInheritNames(Number(row.area))" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overdueCapital" label="本金余额(元)" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueInterest" label="利息余额(元)" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueAmounts" label="核销金额(元)" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overduePeriod" label="逾期期数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="sysCode" label="系统来源" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.encryptSysCode(row.sysCode)">
        </el-table-column>
        <el-table-column prop="loanChannel" label="放款渠道" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.encryptLoanChannel(row.loanChannel)">
        </el-table-column>
        <el-table-column prop="perDueDate" label="还款到期日" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="commonFlag" label="附件是否提交" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.commonFlag,'CommonFlag')">
        </el-table-column>
        <el-table-column prop="verifyStatus" label="核销状态" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.verifyStatus,'VerifyStatus')">
        </el-table-column>
        <el-table-column prop="followInTime" label="案件流入时间" :formatter="(row) => $filter.dateTimeFormat(row.followInTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
      </template>
    </data-box>
  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Auth, Layout, Dependencies } from '~/core/decorator';
import DataBox from '~/components/common/data-box.vue';
import DataForm from "~/components/common/data-form.vue"
import { ReportManageService } from "~/services/domain-service/report-manage.service";
import { switchMap } from 'rxjs/operator/switchMap';
import { CaseVerifyService } from "~/services/domain-service/case-verify.service";
import { PageService } from "~/utils/page.service";

// @Auth(1004)
@Layout('workspace')
@Component({
  components: {
    DataBox,
    DataForm
  }
})
export default class BadDebtDetailReport extends Vue {
  @Dependencies(ReportManageService) private reportManageService: ReportManageService;
  @Dependencies(PageService) private pageService: PageService;
  @Dependencies(CaseVerifyService)
  private caseVerifyService: CaseVerifyService;
  private searchModel: any = {
    caseFollowInTime:'',
    caseNumber:'',
    caseSort:'',
    sysCode:'',
    loanChannel:'',
    verifyBatchNumber:''
  }
  private dataSet: any = []
  private selectionList: any = []
  private submitLoading: any = true

  private refreshData() {
    this.selectionList = []
    const params: any = {
      caseSort: this.searchModel.caseSort,
      sysCode: this.searchModel.sysCode,
      caseNumber: this.searchModel.caseNumber,
      loanChannel: this.searchModel.loanChannel,
      verifyBatchNumber:this.searchModel.verifyBatchNumber,
      caseFollowInTimeStart: this.$filter.dateRanageFormat(this.searchModel.caseFollowInTime).start,
      caseFollowInTimeEnd: this.$filter.dateRanageFormat(this.searchModel.caseFollowInTime).end
    }
    this.reportManageService.getVerifyInfoReport(params,this.pageService)
      .subscribe(data => {
        this.dataSet = data
        if(this.dataSet){
          this.dataSet.forEach(x=>{
            x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
          })
        }
        this.submitLoading = false
        //核销金额 = 本金余额 + 利息余额
        this.dataSet.map(x=>{
          x.overdueAmounts = Number(x.overdueCapital+x.overdueInterest).toFixed(2)
        })
      })
  }
  /**
   * 下载案件
   */
  private downloadVerifiedCase(){
    if(this.dataSet.length === 0){
      this.$message("暂无数据可以导出")
      return
    }
    const ids =this.selectionList? this.selectionList.map((v) => v.id):'';
    const request: any = {
      caseSort: this.searchModel.caseSort,
      sysCode: this.searchModel.sysCode,
      caseNumber: this.searchModel.caseNumber,
      loanChannel: this.searchModel.loanChannel,
      verifyBatchNumber:this.searchModel.verifyBatchNumber,
      caseFollowInTimeStart: this.$filter.dateRanageFormat(this.searchModel.caseFollowInTime).start,
      caseFollowInTimeEnd: this.$filter.dateRanageFormat(this.searchModel.caseFollowInTime).end,
      caseIds: ids,
    }
    this.reportManageService.exportVerifyInfoReport(request).subscribe(
      data=>{
        this.$common.downloadFile(data.url,'核销案件.xls')
      }
    )
  }
  private mounted() {
    this.refreshData()
  }

}
</script>

<style lang="less">

</style>
